package util

import (
	"net/smtp"
	"strings"
	"toy-car/pkg/setting"

	"github.com/jordan-wright/email"
)

//@author: [maplepie](https://github.com/maplepie)
//@function: Email
//@description: Email发送方法
//@param: subject string, body string
//@return: error

func Email(To, subject string, body string) error {
	to := strings.Split(To, ",")
	return send(to, subject, body)
}

//@author: [maplepie](https://github.com/maplepie)
//@function: send
//@description: Email发送方法
//@param: subject string, body string
//@return: error

func send(to []string, subject string, body string) error {

	e := email.NewEmail()
	e.From = setting.EmailObject.From
	e.To = to
	e.Subject = subject
	e.Text = []byte(body)
	return e.Send(setting.EmailObject.Addr, smtp.PlainAuth("", setting.EmailObject.SendEmail, setting.EmailObject.SendPass, setting.EmailObject.SendSTMP))

	// from := global.GlobalConfig.From
	// nickname := global.GlobalConfig.Nickname
	// secret := global.GlobalConfig.Secret
	// host := global.GlobalConfig.Host
	// port := global.GlobalConfig.Port
	// isSSL := global.GlobalConfig.IsSSL

	// auth := smtp.PlainAuth("", from, secret, host)
	// e := email.NewEmail()
	// if nickname != "" {
	// 	e.From = fmt.Sprintf("%s <%s>", nickname, from)
	// } else {
	// 	e.From = from
	// }
	// e.To = to
	// e.Subject = subject
	// e.HTML = []byte(body)
	// var err error
	// hostAddr := fmt.Sprintf("%s:%d", host, port)
	// if isSSL {
	// 	err = e.SendWithTLS(hostAddr, auth, &tls.Config{ServerName: host})
	// } else {
	// 	err = e.Send(hostAddr, auth)
	// }
	// return err
}
